package com.example.oschina.utils

import android.app.Activity
import android.content.Intent
import android.net.Uri
import com.example.oschina.activity.MyWebView
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object Utils {
  private val TAG = Utils::class.java.simpleName
  private val GET_CODE_REQUEST_CODE = 6578
  val REDIRECT_CODE = "redirect_code"


  private const val client_id = "vsk2zMLvp7BPdXudXVxj"
  private const val redirect_uri = "redirect_uri"
  private const val url =
    "https://www.oschina.net/action/oauth2/authorize?response_type=code&client_id=$client_id&redirect_uri=$redirect_uri"

  fun startLoginOsChina(activity: Activity) {
    val uri = Uri.parse(url)
    val i = Intent(activity, MyWebView::class.java)
    i.data = uri
    i.putExtra(Constants.WEB_URL, url)
//    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    activity.startActivityForResult(i, GET_CODE_REQUEST_CODE)
  }

  fun getCodeFromIntent(requestCode: Int, resultCode: Int, data: Intent?): String {
    if (data != null && resultCode == Activity.RESULT_OK && requestCode == GET_CODE_REQUEST_CODE) {
      return data.getStringExtra(REDIRECT_CODE)
    }
    return ""
  }

  fun getDefaultRetrofit(): Retrofit {
    return Retrofit.Builder()
      .addConverterFactory(GsonConverterFactory.create())
      .baseUrl("https://www.oschina.net/").build()
  }
}